package com.turing.post.capitalaccounts.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.capitalaccounts.util.SaleCashWithdrawBill;
import com.turing.post.capitalaccounts.util.SaleCashWithdrawBillDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSWebInput;

/**
 * 销售机构上存/缴现金支出单（SaleCashWithdrawBill）
 * 
 * @author zxp
 * @Jan 6, 2013
 */
@Stateless(mappedName = "capitalaccounts.impl.SaleCashWithdrawBillDaoImpl")
@Remote
@Local
public class SaleCashWithdrawBillDaoImpl implements SaleCashWithdrawBillDao {

	/**
	 * 实体管理器
	 */
	@PersistenceContext
	protected EntityManager _em;

	@Override
	public SaleCashWithdrawBill getSaleCashWithdrawBillById(
			BigInteger drawBillId) {
		return _em.find(SaleCashWithdrawBill.class, drawBillId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SaleCashWithdrawBill> getSaleCashWithdrawBillList(
			PCPSWebInput webInput, String startTranDate, String endTranDate,
			BigInteger startAmount, BigInteger endAmount, String saleBranchNo,
			Integer page) {
		String sql = "select * from SaleCashWithdrawBill where (?1 is null or saleBranchNo = ?1) and (?2 is null or tranDate >= to_date(?2,'yyyy-MM-dd')) and (?3 is null or tranDate <= to_date(?3,'yyyy-MM-dd')) and (?4 is null or amount >= ?4) and (?5 is null or amount <= ?5) order by saleBranchNo asc,tranTime desc";
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		Query query = _em.createNativeQuery(sql, SaleCashWithdrawBill.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);
		query.setParameter(1, saleBranchNo);
		query.setParameter(2, startTranDate);
		query.setParameter(3, endTranDate);
		query.setParameter(4, startAmount);
		query.setParameter(5, endAmount);
		return query.getResultList();
	}

	@Override
	public BigInteger getSaleCashWithdrawBillTotal(PCPSWebInput webInput,
			String startTranDate, String endTranDate, BigInteger startAmount,
			BigInteger endAmount, String saleBranchNo) {
		String sql = "select nvl(count(*),0) from SaleCashWithdrawBill where (?1 is null or saleBranchNo = ?1) and (?2 is null or tranDate >= to_date(?2,'yyyy-MM-dd')) and (?3 is null or tranDate <= to_date(?3,'yyyy-MM-dd')) and (?4 is null or amount >= ?4) and (?5 is null or amount <= ?5)";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, saleBranchNo);
		query.setParameter(2, startTranDate);
		query.setParameter(3, endTranDate);
		query.setParameter(4, startAmount);
		query.setParameter(5, endAmount);
		return new BigInteger(query.getSingleResult().toString());
	}

	@Override
	public void insertSaleCashWithdrawBill(
			SaleCashWithdrawBill saleCashWithdrawBill)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, saleCashWithdrawBill);
	}

	@Override
	public void updateSaleCashWithdrawBill(
			SaleCashWithdrawBill saleCashWithdrawBill)
			throws PCPSApplicationException {
		GeneralDao.update(_em, saleCashWithdrawBill);
	}

	@SuppressWarnings("unchecked")
	@Override
	public SaleCashWithdrawBill getSaleCashWithdrawBillByVoucherNo(
			String voucherNo) {
		String sql = "select * from SaleCashWithdrawBill where VoucherNo=?1";
		Query query = _em.createNativeQuery(sql, SaleCashWithdrawBill.class);
		query.setParameter(1, voucherNo);
		List<SaleCashWithdrawBill> list = query.getResultList();
		if (list == null || list.size() < 1) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public Integer checkSaleCashWithdrawBillVoucherNo(String issueCompanyNo,
			String voucherNo) {
		String sql = "select nvl(count(*),0) from SaleCashWithdrawBill t"
				+ " where t.saleBranchNo like ?1 and t.voucherNo = ?2"
				+ " and t.cancelSaleCashWithdrawBillId is null";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, issueCompanyNo + "%");
		query.setParameter(2, voucherNo);
		return new Integer(query.getSingleResult().toString());
	}
}